# -*- coding: utf-8 -*-
"""
场景1：模拟N个用户并发请求网页，共有5个URL地址，每个虚拟用户都会依次循环加载5个URL地址
"""

from locust import TaskSet, task, HttpUser, constant
import time
import os


class UserTask(TaskSet):
    def __init__(self, parent):
        super().__init__(parent)
        self.index = 0

    def on_start(self):
        pass

    @task
    def test_visit(self):
        print(f'share_data:{self.user.share_data}')
        print(f"index:{self.index}")
        url = self.user.share_data[self.index]
        print(f'visit url: {url}')
        self.client.get(url)
        self.index = (self.index + 1) % len(self.user.share_data)
        print(f"------------------------------------------------")


class WebsiteUser(HttpUser):
    print("88888888888888888888888888888888888888888888888888888888")
    host = 'http://www.baidu.com'
    task_set = task(UserTask)
    share_data = ['/s?wd=apple', '/s?wd=banana', '/s?wd=pear', '/s?wd=orange', '/s?wd=tea']
    wait_time = constant(2)


if __name__ == '__main__':
    current_file_path = os.path.abspath(__file__)
    os.system(f"locust -f {current_file_path}")
